#!/usr/bin/env python

from glob import glob
from tzutil.day import str2day, day2str
from decimal import Decimal
from fetch import fetch_currency_rate
from json import load, dump
from data import import_data


def rate_li(pair):
    rate_dict = import_data(pair)
    li = []
    for k, v in rate_dict.items():
        li.append((int(k), int(v)))
    li.sort(key=lambda x: x[0])
    return li


def main():
    path = 'USD_CNY'
    li = rate_li(path)
    for day, rate in fetch_currency_rate('USD', li[-1][0]):
        rate = int(float(rate) * 10000)
        li.append((day, rate))
        print(day, rate)
    usd_cny = dict(li)
    import_data(path, usd_cny)

    path = 'USD_HKD'
    li = rate_li(path)
    for day, rate in fetch_currency_rate('HKD', li[-1][0]):
        rate = int(usd_cny[day] / float(rate))
        li.append((day, rate))
    import_data(path, dict(li))


if __name__ == "__main__":
    main()
